<template>
	<view style="">
		<j-device-preview ref="expansionUi" setting :device='device' :showHeader="false" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				device: null
			}
		},
		onLoad(options) {
			let enData = decodeURIComponent(options.device)
			this.device = JSON.parse(enData);
		},
		onShow() {
            uni.$on('editDeviceName',(data)=>{
                this.editDevice(data)
            })
            uni.setNavigationBarTitle({
                title: this.device.deviceName
            });
            // mqtt断链超过1分钟重新订阅
            const now = Date.now()
            const prev = uni.getStorageSync('mqttCloseTemp')
            if(prev&&now-prev>=60*1000) {
                uni.$on('mqttConnectSuccess', () => {
                    this.sendWebsocketInfo(this.device.deviceNo)
                })
            }
        },
		onUnload() {},
		methods: {
            editDevice(data){
                this.device.deviceName = data.deviceName
            },
            //websocket发送消息
            sendWebsocketInfo(sns) {
                let topic = `/device/${sns}`;
                let newTopics = `/device/${sns}/alarmEvent`;
                // 订阅单个设备
                getApp().globalData.mqtt.subscribe(topic)
                getApp().globalData.mqtt.subscribe(newTopics)
                this.$logs({info: '超过一分钟-订阅单个设备', topic, newTopics})
            },
        }
	}
</script>

<style lang="less" scoped>

</style>
